<?php 
if(KT!='KhiThe') exit;
class shoppingcarts_model extends khithe_mysql{

	public function getCartIDNganLuong($ID)
	{
		$arrRow = array();
		$ID = parent::checkStrMySQL($ID);
		$select = "	SELECT
						".products.".Title as item_name,
						".shoppingcarts_detail.".Qty as item_quanty,
						".shoppingcarts_detail.".Price as item_amount
					FROM
						".products."
						INNER JOIN ".shoppingcarts_detail." ON ".shoppingcarts_detail.".ProductID = ".products.".ID
						INNER JOIN ".shoppingcarts." ON ".shoppingcarts.".ID = ".shoppingcarts_detail.".ShopingCartID
					WHERE
					".shoppingcarts.".ID = '".$ID."' ";
		$query = parent::SelectMySQL($select);
		
		while ($row = mysql_fetch_assoc($query)){
			$arrRow[] = $row; 	
		}
			
		return $arrRow;
	}
	
	public function getSumPriceCart($ID)
	{
		$arrRow = array();
		$ID = parent::checkStrMySQL($ID);
		$select = "	SELECT

						SUM(".shoppingcarts_detail.".Price * ".shoppingcarts_detail.".Qty) as total
					FROM
						".products."
						INNER JOIN ".shoppingcarts_detail." ON ".shoppingcarts_detail.".ProductID = ".products.".ID
						INNER JOIN ".shoppingcarts." ON ".shoppingcarts.".ID = ".shoppingcarts_detail.".ShopingCartID
					WHERE
					".shoppingcarts.".ID = '".$ID."' ";
		$query = parent::SelectMySQL($select);
		
		$row = mysql_fetch_assoc($query);
			
		return $row['total'];
	}
	
	public function getCartID()
	{
		$select = "SELECT * FROM ".shoppingcarts." ORDER BY ID DESC LIMIT 0,1";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
			
		return $row['ID'];
	}
	public function getPrice($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".products." WHERE ID = '".$ID."' ";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		
		$Price = $row['PriceSpecial']!=''?$row['PriceSpecial']:$row['Price'];
		
		return $Price;
	}
	
	public function insertCart($arr,$arrCart)
	{
		if(!empty($arrCart)){

			$Fullname = parent::checkStrMySQL($arr['txt_Fullname']);
			$Email = parent::checkStrMySQL($arr['txt_Email']);
			$Sex = parent::checkStrMySQL($arr['txt_Sex']);
			$Mobi = parent::checkStrMySQL($arr['txt_Mobi']);
			$CMND = parent::checkStrMySQL($arr['txt_CMND']);
			$Address = parent::checkStrMySQL($arr['txt_Address']);
			$Bank = parent::checkStrMySQL($arr['txt_Bank']);
			$Detail = parent::checkStrMySQL($arr['txt_Detail']);
			
			$select = "INSERT INTO ".shoppingcarts."(Fullname,Email,Sex,Mobiphone,CMND,Bank,Detail,PostDate,Address,`Process`,`Status`) 
											VALUES('".$Fullname."','".$Email."','".$Sex."','".$Mobi."','".$CMND."','".$Bank."','".$Detail."',".time().",'".$Address."','notview','true')";
			$query = parent::UpdateInsertMySQL($select);
			
			$ShopingCartID = mysql_insert_id();
			$_SESSION['CartID'] = $ShopingCartID;
			$tm = time();
			$_SESSION['TimeDate'] = date("Y",$tm).date("m",$tm).date("d",$tm);			
			if(!empty($ShopingCartID)){
				foreach($arrCart as $ProductID=>$Qty){
					
					$Price=$this->getPrice($ProductID);
					$select_detail = "INSERT INTO ".shoppingcarts_detail."(ShopingCartID,ProductID,Qty,Price) VALUES('".$ShopingCartID."','".$ProductID."','".$Qty."','".$Price."')"; 
					$query_detail = parent::UpdateInsertMySQL($select_detail);
					
				}
			}
		}
					
		return $query_detail;
	}
	
	public function setCartOk($ID,$Val='process')
	{
		$ID = parent::checkStrMySQL($ID);
		$update = "UPDATE ".shoppingcarts." SET Process='".$Val."'  WHERE ID = '".$ID."'";
		$update_qr = parent::UpdateInsertMySQL($update);
		parent::CloseConnection();
		return $update_qr;
	}
	
	public function selectProduct($arrID)
	{
        $strID = '';
		if(count($arrID)){
			foreach ($arrID as $valID){
				$strID .= " ID='".$valID."' OR";
			}
			$strID = substr($strID,0,-2);
			$select = "SELECT * FROM ".products." WHERE `Status`='true' AND ( ".$strID." )";
			$query = parent::SelectMySQL($select);
			
			while ($row = mysql_fetch_assoc($query)){
				$arrRow[] = $row; 	
			}
		}
		return $arrRow;
	}
}
?>